package com.mallcloud.mall.app.controller;

import com.alibaba.fastjson.JSONObject;
import com.mallcloud.mall.app.api.entity.dto.WxPay3UnifiedOrderInfoDTO;
import com.mallcloud.mall.app.api.entity.vo.WxPay3UnifiedOrderResponseVO;
import com.mallcloud.mall.app.service.AppOrderCouponService;
import com.mallcloud.mall.app.service.AppWxPay3Service;
import com.mallcloud.mall.common.core.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 *  https://pay.weixin.qq.com/wiki/doc/apiv3/index.shtml
 */
@Api(tags = "app-微信v3支付")
@RestController
@AllArgsConstructor
@RequestMapping("/app/wxPay3")
@ConditionalOnProperty("const.wxpay.v3.appId")
public class AppWxPay3Controller {

	private final AppWxPay3Service appWxPayService;
	private final AppOrderCouponService appOrderCouponService;

	@ApiOperation("微信统一下单")
	@PostMapping("/unifiedOrder")
	public R<WxPay3UnifiedOrderResponseVO> unifiedOrder(@RequestBody WxPay3UnifiedOrderInfoDTO unifiedOrderInfo) {
		return R.ok(appWxPayService.unifiedOrder(unifiedOrderInfo));
	}

	// 百度查询 微信支付V3 wechatpay-apache-httpclient
	// https://blog.csdn.net/weixin_48855888/article/details/127491496
	@ApiOperation("支付异步通知接收")
	@PostMapping("/payNotify")
	public JSONObject payNotify(HttpServletRequest request, HttpServletResponse response) {
		return appWxPayService.payNotify(request, response);
	}

	@ApiOperation("退款异步通知接收")
	@PostMapping("/refundNotify")
	public JSONObject refundNotify(HttpServletRequest request, HttpServletResponse response) {
		return appWxPayService.refundNotify(request, response);
	}

	@ApiOperation("回调失败补偿")
	@PostMapping("/compensate")
	public void compensate(@RequestBody List<Long> ids) {
		appOrderCouponService.compensate(ids);
	}
}
